package com.tddx.activiti.utils.gentiJava;

import java.io.FileInputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.tddx.activiti.utils.ConstantsPath;
import com.tddx.activiti.utils.StringUtils;

public class Constants {

	public static String URL = "jdbc:oracle:thin:@192.168.10.6:1521:csmis";
	public static String NAME = "csmiswhshow";
	public static String PASS = "csmiswh";
	public static String DRIVER = "oracle.jdbc.driver.OracleDriver";
	public static Statement PSTEMT = null;
	public static String TABLENAME = "user_col_comments";
	public static String TOPPACKNAME = "com.tddx.activiti.business.core";
	public static String PREJAVANAME = "";//table转换过来的java的名称未加后缀entity、dao等
	public static String PACKAGENAME = "";//controller或者dao或者service下面的包，名称一样
	
	public static String CONTROLLERJAVAPATH = "";
	public static String SERVICEJAVAPATH = "";
	public static String REPOSITORYJAVAPATH = "";
	public static String ENTITYJAVAPATH = "";
	public static String JDBCJAVAPATH = "";
	public static String RESULTJSPPATH = "";
	public static String INPUTJSPPATH = "";
	
	public static String CONTROLLERJAVANAME = "";
	public static String SERVICEJAVANAME = "";
	public static String REPOSITORYJAVANAME = "";
	public static String ENTITYJAVANAME = "";
	public static String JDBCJAVANAME = "";
	public static String RESULTJSPNAME = "";
	public static String INPUTJSPNAME = "";

	public static String CONTROLLERPACKAGE = "";
	public static String SERVICEPACKAGE = "";
	public static String ENTITYPACKAGE = "";
	public static String REPOSITORYPACKAGE = "";
	
	public static List<TablePro> tableProlist = null;
	public static String sqlList = "";
	
	static{
		new Constants().refreshData();
	}
	
	public void refreshData() {
		Properties properties = new Properties();
		String path = Thread.currentThread().getContextClassLoader()
				.getResource("props/config.properties").getPath();
		try {
			properties.load(new FileInputStream(path));
			URL = properties.getProperty("jdbc.url");
			NAME = properties.getProperty("jdbc.username");
			PASS = properties.getProperty("jdbc.password");
			DRIVER = properties.getProperty("jdbc.driverClassName");
			Class.forName(DRIVER);
			TABLENAME = StringUtils.lowerCase(TABLENAME);
			PREJAVANAME = StringUtils.capitalizeBySymbol(TABLENAME, "_", "\\$");
			PACKAGENAME = StringUtils.lowerCase(PREJAVANAME);
			String topPath = ConstantsPath.getRootPath() + "\\src\\main\\java\\" + TOPPACKNAME.replace(".", "\\");
			String jspPath = ConstantsPath.getRootPath() + "\\src\\main\\webapp\\modules" + 
					TOPPACKNAME.substring(TOPPACKNAME.lastIndexOf(".") + 1) + PACKAGENAME;
			
			CONTROLLERJAVANAME = PREJAVANAME + "Ctrl";
			SERVICEJAVANAME = PREJAVANAME + "Service";
			REPOSITORYJAVANAME = PREJAVANAME + "Dao";
			ENTITYJAVANAME = PREJAVANAME + "Entity";
			JDBCJAVANAME = PREJAVANAME + "Jdbc";
			
			CONTROLLERJAVAPATH = topPath + "\\controller\\" + PACKAGENAME + "\\" + PREJAVANAME + "Ctrl.java";
			SERVICEJAVAPATH = topPath + "\\service\\" + PACKAGENAME + "\\" + PREJAVANAME + "Service.java";
			REPOSITORYJAVAPATH = topPath + "\\dao\\" + PACKAGENAME + "\\" + PREJAVANAME + "Dao.java";
			ENTITYJAVAPATH = topPath + "\\entity\\" + PACKAGENAME + "\\" + PREJAVANAME + "Entity.java";
			JDBCJAVAPATH = topPath + "\\dao\\" + PACKAGENAME + "\\" + PREJAVANAME + "Jdbc.java";
			System.out.println("entity path..." + ENTITYJAVAPATH);
			RESULTJSPPATH = jspPath + PACKAGENAME + "List.jsp";
			INPUTJSPPATH = jspPath + PACKAGENAME + "Input.jsp";;
			
			CONTROLLERPACKAGE = CONTROLLERJAVAPATH.substring(CONTROLLERJAVAPATH.indexOf("src\\") + 4
					, CONTROLLERJAVAPATH.lastIndexOf("\\")).replace("\\", ".");
			SERVICEPACKAGE = SERVICEJAVAPATH.substring(SERVICEJAVAPATH.indexOf("src\\") + 4
					, SERVICEJAVAPATH.lastIndexOf("\\")).replace("\\", ".");
			ENTITYPACKAGE = ENTITYJAVAPATH.substring(ENTITYJAVAPATH.indexOf("src\\") + 4
					, ENTITYJAVAPATH.lastIndexOf("\\")).replace("\\", ".");
			REPOSITORYPACKAGE = REPOSITORYJAVAPATH.substring(REPOSITORYJAVAPATH.indexOf("src\\") + 4
					, REPOSITORYJAVAPATH.lastIndexOf("\\")).replace("\\", ".");
			
			tableProlist = getColumnList();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}

	public static void main(String[] args) {
		getColumnList();
	}
	/**
	 * 获取列值封装到List<TablePro>
	 * @return
	 */
	public static List<TablePro> getColumnList() {

		String sql = "select a.COLUMN_NAME,a.DATA_TYPE,a.DATA_LENGTH,a.NULLABLE,a.DATA_DEFAULT,b.comments"
				+ " from user_tab_cols a, user_col_comments b where a.TABLE_NAME=b.table_name"
				+ " and a.COLUMN_NAME=b.column_name and  a.Table_name=upper('"
				+ Constants.TABLENAME + "')";
		String sql2 = "select a.COLUMN_NAME"
				+ " from user_cons_columns a, user_constraints b"
				+ " where a.constraint_name = b.constraint_name and b.constraint_type = 'P' and  a.Table_name=upper('"
				+ Constants.TABLENAME + "')";
		List<TablePro> list = new ArrayList<TablePro>();
		try {
			Statement pStemt = ConnDb.getConn().createStatement();
			Statement pStemt2 = ConnDb.getConn().createStatement();
			ResultSet rs = pStemt.executeQuery(sql);
			ResultSet rs2 = pStemt2.executeQuery(sql2);
			Map<String, String> map = new HashMap<String, String>();
			while (rs2.next()){
				map.put(rs2.getString(1), "1");
			}
			while (rs.next()) {
				TablePro tp = new TablePro();
				tp.setColName(rs.getString(1));
				tp.setDataType(rs.getString(2));
				tp.setDataLength(rs.getString(3));
				tp.setNullAble(rs.getString(4));
				tp.setDataDefault(rs.getString(5));
				tp.setComments(rs.getString(6));
				tp.setIsPrimary(map.get(rs.getString(1)));
				list.add(tp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public static String getSqlList(){
//		String sql ="";
		if(tableProlist==null){
			tableProlist = getColumnList();
		}
		return "";
	}
	
}
